package com.atguigu.gmall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsConfigurationSource;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * @author mqx
 * @date 2020-9-29 10:32:45
 */
@Configuration // xxx.xml
public class CorsConfig {

    // 自定义一个实体类对象，将这个实体对象注入到spring 容器中！
    // CorsWebFilter
    @Bean
    public CorsWebFilter getCorsWebFilter(){
        // 创建CorsConfiguration
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedHeader("*"); // 请求头
        corsConfiguration.addAllowedMethod("*"); // 请求方法
        corsConfiguration.addAllowedOrigin("*"); // 请求url
        corsConfiguration.setAllowCredentials(true); // 允许携带cookie
        UrlBasedCorsConfigurationSource configurationSource = new UrlBasedCorsConfigurationSource();

        // 设置参数
        configurationSource.registerCorsConfiguration("/**",corsConfiguration);
        // 返回
        return new CorsWebFilter(configurationSource);
    }
}
